package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.CategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理类别相关请求的控制器
 *
 * @author Chen
 * @since 2023/2/2 16:18:18
 */
@Slf4j
@RestController
@RequestMapping("/categories")
@Tag(name = "01. 类别管理模块")
@Validated
public class CategoryController {

    private final CategoryService categoryService;

    @Autowired
    public CategoryController(CategoryService categoryService) {
        log.debug("创建控制器对象：CategoryController");
        this.categoryService = categoryService;
    }

    // http://localhost:8088/categories/add-new
    @ApiOperationSupport(order = 100)
    @Operation(summary = "添加类别")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(CategoryAddNewDTO categoryAddNewDTO) {
        log.debug("开始处理【添加类别】的请求，参数：{}", categoryAddNewDTO);
        categoryService.addNew(categoryAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8088/categories/9527/delete
    @Parameter(name = "id", description = "类别ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 200)
    @Operation(summary = "根据id删除类别")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1L, max = 10000L, message = "删除类别失败，类别ID非法！") @PathVariable Long id) {
        log.debug("开始处理【根据id删除类别】的请求，参数：{}", id);
        categoryService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:8088/categories/9527/enable
    @Parameter(name = "id", description = "类别ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 310)
    @Operation(summary = "启用类别")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@Range(min = 1L, max = 10000L, message = "启用类别失败，类别ID非法！") @PathVariable Long id) {
        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:8088/categories/9527/disable
    @Parameter(name = "id", description = "类别ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 311)
    @Operation(summary = "禁用类别")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@Range(min = 1L, max = 10000L, message = "禁用类别失败，类别ID非法！") @PathVariable Long id) {
        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:8088/categories/9527
    @Parameter(name = "id", description = "类别ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 400)
    @Operation(summary = "根据id查询类别详情")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<CategoryStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询类别详情】的请求，参数：{}", id);
        CategoryStandardVO categoryStandardVO = categoryService.getStandardById(id);
        return JsonResult.ok(categoryStandardVO);
    }

    // http://localhost:9080/categories
    @ApiOperationSupport(order = 410)
    @Operation(summary = "查询类别列表")
    @GetMapping("")
    public JsonResult<List<CategoryListItemVO>> list() {
        log.debug("开始处理【查询类别列表】的请求，无参数");
        List<CategoryListItemVO> list = categoryService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:8088/categories/list-by-parent
    @ApiOperationSupport(order = 420)
    @Operation(summary = "根据父级查询子级类别列表")
    @GetMapping("/list-by-parent")
    public JsonResult<List<CategoryListItemVO>> listByParentId(Long parentId) {
        log.debug("开始处理【根据父级查询子级类别列表】的业务，无参数");
        List<CategoryListItemVO> list = categoryService.listByParentId(parentId);
        return JsonResult.ok(list);
    }

}
